﻿Imports System
Imports System.IO
Imports System.Net
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Media
Imports System.Windows.Media.Animation
Imports System.Windows.Navigation
Imports System.Windows.Threading

Partial Public Class MessageWindow

    Friend Shared ForceClose As Boolean
    Private Timer As DispatcherTimer


    Public Sub New()
        MyBase.New()

        Me.InitializeComponent()

        ForceClose = False
        Topmost = False

        AddHandler WpfApp.Current.MainWindow.Activated, AddressOf WpfAppCurrentMainWindowActivated
        Timer = New DispatcherTimer(DispatcherPriority.Background, Dispatcher)

        AddHandler Timer.Tick, AddressOf TimerTick
        Timer.Interval = TimeSpan.FromMilliseconds(500)
        Timer.Start()

    End Sub

    Private Sub Button_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)
        'TODO: Add event handler implementation here.
        Close()
    End Sub

    Private Sub Thumb_DragDelta(ByVal sender As Object, ByVal e As System.Windows.Controls.Primitives.DragDeltaEventArgs)
        'TODO: Add event handler implementation here.
        Top += e.VerticalChange
        Left += e.HorizontalChange
    End Sub

    Private Sub Border_MouseLeftButtonDown(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
        'TODO: Add event handler implementation here.
        windowdragthumb.RaiseEvent(e)
    End Sub

    Private Sub Grid_MouseLeftButtonDown(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
        'TODO: Add event handler implementation here.
        windowdragthumb.RaiseEvent(e)
    End Sub

    Private Sub UniformGrid_MouseLeftButtonDown(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
        'TODO: Add event handler implementation here.
        windowdragthumb.RaiseEvent(e)
    End Sub

    Public Property TitleCustom As String
        Get
            Return titleTextBlock.Text
        End Get
        Set(ByVal value As String)
            titleTextBlock.Text = value
        End Set
    End Property

    Public Property ShowCloseButton As Boolean
        Get
            Return If(button.Visibility = Windows.Visibility.Visible, True, False)
        End Get
        Set(ByVal value As Boolean)
            button.Visibility = If(value, Visibility.Visible, Visibility.Hidden)
        End Set
    End Property

    Private Sub TimerTick(ByVal sender As Object, ByVal e As EventArgs)
        If ForceClose Then
            Timer.Stop()
            RemoveHandler Timer.Tick, AddressOf TimerTick
            Close()
        End If
    End Sub

    Private Sub WpfAppCurrentMainWindowActivated(ByVal sender As Object, ByVal e As EventArgs)
        Activate()
    End Sub





    Private Sub MessageWindow_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Me.Closing
        Try
            RemoveHandler WpfApp.Current.MainWindow.Activated, AddressOf WpfAppCurrentMainWindowActivated
        Catch ex As NullReferenceException

        End Try
    End Sub

    Dim previousleft As Double
    Dim previoustop As Double
    Private Sub MessageWindow_LocationChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LocationChanged
        'Dim frect = New Rect(Left, Top, ActualWidth, ActualHeight)
        ''Dim rect = New Rect(SystemParameters.VirtualScreenLeft, _
        ''                    SystemParameters.VirtualScreenTop, _
        ''                    SystemParameters.VirtualScreenWidth, _
        'Dim rect = System.Windows.Rect.Inflate(System.Windows.SystemParameters.WorkArea, 0, -10)


        'If Not rect.Contains(frect) Then
        '    Left = previousleft
        '    Top = previoustop
        'End If

        'previousleft = Left
        'previoustop = Top
    End Sub
End Class
